package com.profgj.profgj.controller;

import com.profgj.profgj.service.ProjectTotalEnergyconsumptionService;
import com.profgj.profgj.utils.EmptyUtils;
import com.profgj.profgj.utils.ReturnOperator;
import com.profgj.profgj.vo.ProjectTotalInfoResult;
import com.profgj.profgj.vo.ProjectTotalInfoVo;
import com.profgj.profgj.vo.ReturnResult;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目的总能耗统计获取
 * 项目的总能耗统计的记录添加是经过产品的月能耗添加或者更变才进行 统计运算添加到数据中的
 * 是经过ProductMonthEnergyController 中的 POST和put 添加或更变之后进行统计 调用的服务层ProjectTotalEnergyconsumptionServiceImpl服务类中
 * projectTotalEnergyConsumption 这个方法实现的
 */
@RestController
@RequestMapping("/statis")
@Api(tags = {"公司旗下的所有产品年统计集合运算统计"},description = "公司旗下的所有产品年统计集合运算统计")
public class ProjectStatisticsController {


    /**
     * 项目的总能耗服务对象
     */
    @Resource
    private ProjectTotalEnergyconsumptionService projectTotalEnergyconsumptionService;


    /**
     * 项目总能耗和附加展示信息
     * 企业+项目+ 产品年产量+产品销售+ 产品总能耗+产品单位产品能耗+ 产品能耗强度+项目总能耗+能耗替代量+ 十三五+十四五
     * @return
     */
    @GetMapping("/statistic")
    @ApiOperation(value = "",produces = "application/json",consumes = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "draw",value = "绘制次数", required = true, dataType = "Integer", paramType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "start",value = "查询起始位置", required = true, dataType = "Integer", paramType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "length",value = "查询条数", required = true, dataType = "Integer", paramType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "search",value = "关键字", required = true, dataType = "String", paramType = "String", dataTypeClass = String.class)
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "表明该请求被成功地完成,所请求的资源发送到客户端"),
            @ApiResponse(code = 401, message = "请求要求身份验证,常见于需要登录而用户未登录的情况"),
            @ApiResponse(code = 403, message = "服务器拒绝请求,常见于机密信息或复制其它登录而用户未登录的情况"),
            @ApiResponse(code = 404, message = "服务器无法取得所请求的网页,请求资源不存在"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public ResponseEntity<ReturnResult> StatisticsMethod(Integer draw,
                                                         Integer start,
                                                         Integer length,
                                                         String search,
                                                         @ApiIgnore HttpSession session){

        // 参数校验
        if (!EmptyUtils.isIntegerEmpty(start) && !EmptyUtils.isIntegerEmpty(length)) {
            return ReturnOperator.createResultMsg(draw, 0, 0,
                    new ArrayList(), HttpStatus.OK);
        }

        Integer role_id = Integer.valueOf(String.valueOf(session.getAttribute("role_id")));
        Integer user_id = Integer.valueOf(String.valueOf(session.getAttribute("user_id")));
        // 查询项目总能耗 + 年能耗部分字段 + 企业名+ 项目名+ 产品名 list列表
        List<ProjectTotalInfoVo> projectTotalInfoVoList = null;
        // 对分页web页面项目总能耗展示查询的list列进行 sum统计对象
        ProjectTotalInfoResult projectTotalInfoResult = null;
        Integer totalConut = 0;
        // 不是管理员的情况下
        if(role_id != 1) {

            if (EmptyUtils.isStringEmpty(search)) {
                // 关键字,用户id查询总记录数
                totalConut = projectTotalEnergyconsumptionService.queryProjectTotalInfoByKeywordAndUserId(search,user_id).size();
                // 查询项目总能耗 + 年能耗部分字段 + 企业名+ 项目名+ 产品名
                projectTotalInfoVoList = projectTotalEnergyconsumptionService.queryProjectTotalInfoByKeywordLimitAndUserId(search, start, length,user_id);
            } else {
                // 用户id查询总记录数
                totalConut = projectTotalEnergyconsumptionService.queryProjectTotalInfoByUserId(user_id).size();
                // 查询项目总能耗 + 年能耗部分字段 + 企业名+ 项目名+ 产品名
                projectTotalInfoVoList = projectTotalEnergyconsumptionService.queryProjectTotalInfoByLimitAndUserId(start, length,user_id);
            }

        } else {

            if (EmptyUtils.isStringEmpty(search)) {
                // 关键字查询总记录数
                totalConut = projectTotalEnergyconsumptionService.queryProjectTotalInfoByKeyword(search).size();
                // 查询项目总能耗 + 年能耗部分字段 + 企业名+ 项目名+ 产品名
                projectTotalInfoVoList = projectTotalEnergyconsumptionService.queryProjectTotalInfoByKeywordLimit(search, start, length);
            } else {
                // 无关键字查询总记录数
                totalConut = projectTotalEnergyconsumptionService.queryProjectTotalInfoAll().size();
                // 查询项目总能耗 + 年能耗部分字段 + 企业名+ 项目名+ 产品名
                projectTotalInfoVoList = projectTotalEnergyconsumptionService.queryProjectTotalInfoByLimit(start, length);

            }

        }

        // 查询结果校验
        if (!EmptyUtils.isProjectTotalInfoVoListEmpty(projectTotalInfoVoList)) {
            return ReturnOperator.createResultMsg(draw, 0, 0,
                    new ArrayList(), HttpStatus.OK);
        }

        // 返回数据
        return ReturnOperator.createResultMsg(draw, totalConut, totalConut,
                projectTotalInfoVoList, HttpStatus.OK);
    }

}
